package com.education.wisdom.controller.app;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.education.common.utils.LogUtil;
import com.education.wisdom.pojo.PersonalCatalog;
import com.education.wisdom.pojo.PersonalResources;
import com.education.wisdom.service.IPersonalCatalogService;
import com.education.wisdom.service.IPersonalResourcesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;

import static com.education.common.core.BaseController.writeJSON;

/**
 * 个人资源库 信息操作处理
 *
 * @author ruoyi
 * @date 2019-06-05
 */
@Controller
@CrossOrigin
@Slf4j
@ResponseBody
@Api(tags = {"个人资源库"})
@RequestMapping("/personalResources")
public class PersonalResourcesController  {

    @Autowired
    private IPersonalResourcesService personalResourcesService;

    @Autowired
    private IPersonalCatalogService personalCatalogService;

    /**
     * 查询私有资源接口
     *
     * @param response
     * @param personalResources
     * @param pageSize
     * @param pageNo
     * @throws IOException
     */
    @GetMapping("/list")
    @ApiOperation("查询私有资源接口")
    @ApiImplicitParams({@ApiImplicitParam(name = "rCatalogid", value = "目录编号", dataType = "Integer"),
            @ApiImplicitParam(name = "userId", value = "用户ID", dataType = "String")})
    public void list(HttpServletResponse response, PersonalResources personalResources, Integer pageSize, Integer pageNo) throws IOException {
        log.info(LogUtil.LogInfo("查询私有资源目录", new Date()));
        PersonalCatalog personalCatalog = personalCatalogService.selectPersonalCatalogById(personalResources.getrCatalogid());
        if (personalCatalog != null) {
            if (personalCatalog.getParentId() != 0) {
                PageHelper.startPage(pageNo, pageSize);
                List<PersonalResources> list = personalResourcesService.selectPersonalResourcesList(personalResources);
                writeJSON(new PageInfo<>(list), response, "200", "success");
            } else {
                PageHelper.startPage(pageNo, pageSize);
                List<PersonalResources> list = personalResourcesService.selectByUserIdAndCatalogId(personalResources);
                writeJSON(new PageInfo<>(list), response, "200", "success");
            }
        } else {
            PageHelper.startPage(pageNo, pageSize);
            List<PersonalResources> personalResources1 = personalResourcesService.selectPersonalResourcesList(personalResources);
            writeJSON(new PageInfo<>(personalResources1), response, "200", "success");
        }
    }

    /**
     * 上传资源
     *
     * @param response
     * @param personalResources
     * @throws IOException
     */
    @PostMapping("/ins")
    @ApiOperation("上传资源")
    @ApiImplicitParams({@ApiImplicitParam(name = "rCatalogid", value = "目录编号", dataType = "Integer"),
            @ApiImplicitParam(name = "rUrl", value = "路径", dataType = "String"),
            @ApiImplicitParam(name = "rName", value = "名称标题", dataType = "String"),
            @ApiImplicitParam(name = "rRemark", value = "描述", dataType = "String"),
            @ApiImplicitParam(name = "userId", value = "用户ID", dataType = "String")})
    public void ins(HttpServletResponse response, @RequestBody PersonalResources personalResources) throws IOException {
        log.info(LogUtil.LogInfo("上传资源", new Date()));
        if (personalResources.getrCatalogid() != null && personalResources.getUserId() != null && personalResources.getrUrl() != null) {
            personalResources.setCreateTime(new Date());
            personalResourcesService.insertPersonalResources(personalResources);
            writeJSON("上传成功", response, "200", "success");
        } else {
            writeJSON("信息不全", response, "201", "success");
        }
    }

    @PostMapping("/listIns")
    public void directoryUploadResources(HttpServletResponse response, PersonalResources personalResources, String usrls, String names) throws IOException {
        log.info(LogUtil.LogInfo("上传资源", new Date()));
        if (usrls.contains(",")) {
            String[] url = usrls.split(",");
            String[] name = names.split(",");
            for (int i = 0; i < url.length; i++) {
                personalResources.setrUrl(url[i]);
                personalResources.setrName(name[i]);
                personalResources.setCreateTime(new Date());
                personalResourcesService.insertPersonalResources(personalResources);
            }
        } else {
            personalResources.setrUrl(usrls);
            personalResources.setrName(names);
            personalResources.setCreateTime(new Date());
            personalResourcesService.insertPersonalResources(personalResources);
        }
        writeJSON("上传成功", response, "200", "success");
    }

}
